<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 青 <99701759@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
// 微信用户表中的信息在$this->userinfo
// 用户newmember表的信息在$this->newmember中
// 用户角色表的信息在$this->role中
namespace app\web\controller;

use app\web\api\UserApi;
use app\src\user\action\LoginAction;
use app\src\admin\api\po\SecurityCodePo;
use app\src\admin\api\SecurityCodeApi;
use app\src\securitycode\model\SecurityCode;
use app\src\admin\helper\AdminSessionHelper;
use app\src\user\logic\MemberConfigLogic;
use app\src\user\action\RegisterAction;
class login extends Weixin{


    protected function _initialize() {
        parent::_initialize();
        if (session("?global_user")) {
            $this->userinfo = session("global_user");
        }else{
            $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            $this->getWxuser($url);
            $this->userinfo = session("global_user");
        }
    }

    /*
     * 首页
     * */
    public function forgetpw(){
        if (session("?memberinfo")) {
            $this->redirect('index/index');
        }
        $this->assignTitle('忘记密码');
        if(IS_POST) {
            $mobile=$this->_param('mobile');
            $password=$this->_param('password');
            $code=$this->_param('code');
            $result = UserApi::updatePwd($mobile,$password,$code);
            if (!$result['status']) {
                $this->error('修改失败','',$result['info']);
            }else{
               // session("memberinfo",$result['info']);
                $this->success('修改成功', '');
            }
        }
        return $this->fetch();
    }


    protected function escapeEmoji($strText, $bool = false) {
        $preg = '/\\\ud([8-9a-f][0-9a-z]{2})/i';
        if ($bool == true) {
            $boolPregRes = (preg_match($preg, json_encode($strText, true)));
            return $boolPregRes;
        } else {
            $strPregRes = (preg_replace($preg, '', json_encode($strText, true)));
            $strRet = json_decode($strPregRes, JSON_OBJECT_AS_ARRAY);

            if ( is_string($strRet) && strlen($strRet) == 0) {
                return "";
            }

            return $strRet;
        }
    }


    public function register()
    {
        if (session("?memberinfo")) {
            $this->redirect('index/index');
        }
        $this->assignTitle('注册');
        if(IS_POST) {
            $nickname=$this->_param('mobile');
            $username=$this->_param('mobile');
            $password=$this->_param('password');
            $code=$this->_param('code');
            $invite_code=$this->_param('invite_code');
            $email=$this->_param('email','');
            $wxopenid=$this->userinfo['info']['openid'];
            $result = UserApi::register($nickname,$username,$password,$email,$code,$invite_code,$wxopenid);
            if (!$result['status']) {
                $this->error($result['info'],'');
            }else{
                session("memberinfo",$result['info']);
                $this->success('注册成功', '');
            }
        }
        $invite_code=$this->_param('invite_code');
        $this->assign('invite_code', $invite_code);
        return $this->fetch();
    }

    public function register_auto()
    {

        $this->assignTitle('注册');
        $nickname = $this->userinfo['info']['nickname'];
        $nickname = $this->escapeEmoji($nickname);
        $nickname = mb_substr($nickname,0,16,'utf-8');
        $username = $this->userinfo['info']['openid'];
        $username = str_replace('-','_',$username);
        $password = '666666';
        $invite_code = $this->_param('invite_code','itboye');
        $email = ' ';
        $wxopenid=$this->userinfo['info']['openid'];
//        dump($wxopenid);
//        dump($invite_code);
//        dump($nickname);
//        dump($username);
//        exit;
        $result = (new RegisterAction)->registerProcess($nickname,$username,'+86',$password,$email,$invite_code,$wxopenid);
        if (!$result['status']) {
           // slog($result['info'], '', 'hdg');
            $this->error($result['info'],'');
        }else{
          //  session("memberinfo",$result['info']);
            if($invite_code == 'itboye'){
                $this->redirect('index/index');
            }else{
                $seller_id = $this->_param('seller_id');
                if(!empty($seller_id)){
                    $this->redirect('shop/shopdetail',['invite_code'=>$invite_code,'seller_id'=>$seller_id]);
                }else{
                    $this->redirect('index/index');
                }

            }
        }
        return $this->fetch();
    }

    public function getcode(){
        $code_type = 1;

        $code=$this->_param('code');
        $result = $this -> check_verify($code);
        if(!$result['status']){
            $this->error('获取失败验证码错误', '',$result['info']);
        }
        $mobile=$this->_param('mobile');
        $result = UserApi::code($mobile,$code_type);
        if($result['status']) {
            $this->success('短信已发送', '',$result['info']);
        }else{
            $this->error('发送失败', '',$result['info']);
        }

    }
    public function getcode2(){
        $code_type = 2;
        $code=$this->_param('code');
        $mobile=$this->_param('mobile');
        $result = $this -> check_verify($code);
        if(!$result['status']){
            $this->error('获取失败验证码错误', '',$result['info']);
        }
        $result = UserApi::code($mobile,$code_type);
        if($result['status']) {
            $this->success('短信已发送', '',$result['info']);
        }else{
            $this->error('发送失败', '',$result['info']);
        }

    }
    public function login(){
        $this->assignTitle('登录');
        if (session("?memberinfo")) {
            $this->redirect('index/index');
        }
        if(IS_POST) {
            $mobile = $this->_param('mobile');
            $password = $this->_param('password');
            $result = UserApi::login($mobile, $password, 'device_id_um','pc','+86');
            if ($result['status']) {
                session("memberinfo",$result['info']);
               $uid =$result['info']['uid'];
                $openid =$this->userinfo['info']['openid'];
                (new MemberConfigLogic())->save(['uid'=>$uid],['wxopenid'=>$openid]);
                $this->success('登录成功', '', $result['info']);
            } else {
                $this->error('登录失败', '', $result['info']);
            }
        }
        return $this->fetch();
    }

    private function check_verify($code)
    {
        $acceptor = AdminSessionHelper::getSessionId();
        $po = new SecurityCodePo();
        $po->setAcceptor($acceptor);
        $po->setCodeType(SecurityCode::TYPE_FOR_LOGIN);
        $po->setCode($code);
        $api = new SecurityCodeApi();
        return $api->check($po);
    }


}